package com.dgut.hisystem.service.impl;

import com.dgut.hisystem.entity.*;
import com.dgut.hisystem.entity.VO.PrescriptionVO;
import com.dgut.hisystem.entity.VO.SimpleDrug;
import com.dgut.hisystem.mapper.AdviceMapper;
import com.dgut.hisystem.mapper.DoctorMapper;
import com.dgut.hisystem.mapper.DrugMapper;
import com.dgut.hisystem.mapper.PatientMapper;
import com.dgut.hisystem.service.CaseService;
import com.dgut.hisystem.service.DoctorService;
import com.dgut.hisystem.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.print.Doc;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class DrugServiceImpl implements DrugService {
    @Resource
    private DrugMapper drugMapper;

    @Resource
    private CaseService caseService;
    @Resource
    private PatientMapper patientMapper;
    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private AdviceMapper adviceMapper;

    @Override
    public List<Drug> getAll(){
        return drugMapper.getAll();
    }

    @Override
    public List<Drug> getDrug(long id){
        return drugMapper.getDrug(id);
    }

    @Override
    public List<Drug> getExpDrug(){
        List<Drug> All = drugMapper.getAll();
        List<Drug> ExpDrug = null;
        for (Drug drug : All) {
            Timestamp DrugDate = drug.getYp_datetime();
            Timestamp timeNow = new Timestamp(System.currentTimeMillis());
            long t1 = timeNow.getTime();
            long t2 = DrugDate.getTime();
            int dateDif = (int) ((t2 - t1) / (1000 * 60 * 60 * 24));
            if (dateDif >= 7) {
                ExpDrug.add(drug);
            }
        }
        return ExpDrug;
    }

    @Override
    public Drug getDrugById(long id){
        return drugMapper.getDrugById(id);
    }

    @Override
    public Drug getDrugByName(String name){
        return drugMapper.getDrugByName(name);
    }

    @Override
    public void delete(Integer id){
        drugMapper.delete(id);
    }

    @Override
    public void updatePrice(Integer id, Double price){
        drugMapper.updatePrice(id,price);
    }

    @Override
    public void updateAmount(Integer id, Integer amount){
        drugMapper.updateAmount(id,amount);
    }

    @Override
    public void insertDrug(Drug d){
        drugMapper.insertDrug(d);
    }

    @Override
    public List<PrescriptionVO> selectAllPrescription(){
        List<Prescription> prescriptions = drugMapper.selectAllPrescription();

        List<PrescriptionVO> res = new ArrayList<>();

        for(Prescription prescription : prescriptions){
            // 先搜病历以求双方信息
            Case _case = caseService.selectID(prescription.getBl_id());
            Patient patient = patientMapper.selectID(_case.getBr_card());
            Doctor doctor = doctorMapper.getDoctorId(_case.getYs_id());

            PrescriptionVO temp_prescriptionVO = new PrescriptionVO();
            temp_prescriptionVO.setBr_card(patient.getBr_card());
            temp_prescriptionVO.setBr_name(patient.getBr_name());
            temp_prescriptionVO.setYs_name(doctor.getYs_name());
            temp_prescriptionVO.setYs_office(doctor.getYs_office());
            temp_prescriptionVO.setCf_id(prescription.getCf_id());


            res.add(temp_prescriptionVO);
//            long id = prescriptionVO.getBl_id();
//            long card = drugMapper.SelectBrcardByBlid(id);
//            long ysid = drugMapper.SelectYsidByBlid(id);
//            long yhid = drugMapper.selectYhidByBrcard(card);
//            long ysyhid = drugMapper.selectYhidByYsid(ysid);
//            String ysoffice = drugMapper.selectYsofficeByYsid(ysid);
//            String yhname = drugMapper.selectYhnameByYhid(yhid);
//            String ysname = drugMapper.selectYhnameByYhid(ysyhid);
//            prescriptionVO.setBr_card(card);
//            prescriptionVO.setBr_name(yhname);
//            prescriptionVO.setYs_name(ysname);
//            prescriptionVO.setYs_office(ysoffice);

        }
        return res;

    }

    @Override
    public List<SimpleDrug> drugOutStorage(long cf_id) {
        // 找出处方id绑定的多个医嘱
        List<Advice> adviceList = adviceMapper.getAdviceByPrescription(cf_id);
        // 找到医嘱对应的药品，减去医嘱上写的数量
        List<SimpleDrug> simpleDrugs = new ArrayList<>();
        for (Advice advice : adviceList) {
            drugMapper.subAmount(advice.getYp_id(), (int) advice.getYz_amount());
            SimpleDrug simpleDrug = new SimpleDrug();
            simpleDrug.setYp_id(advice.getYp_id());
            simpleDrug.setSub((int) advice.getYz_amount());
            simpleDrugs.add(simpleDrug);
        }
        adviceMapper.donePrescription(cf_id);

        return simpleDrugs;
    }

    @Override
    public long SelectBrcardByBlid(long bl_id){
        return drugMapper.SelectBrcardByBlid(bl_id);
    }

    @Override
    public long SelectYsidByBlid(long bl_id){
        return drugMapper.SelectYsidByBlid(bl_id);
    }

    @Override
    public long selectYhidByBrcard(long bl_card){
        return drugMapper.selectYhidByBrcard(bl_card);
    }

    @Override
    public long selectYhidByYsid(long ys_id){
        return drugMapper.selectYhidByYsid(ys_id);
    }

    @Override
    public String selectYhnameByYhid(long yh_id){
        return drugMapper.selectYhnameByYhid(yh_id);
    }

    @Override
    public String selectYsofficeByYsid(long ys_id){return drugMapper.selectYsofficeByYsid(ys_id);}

    @Override
    public Advice selectAdviceById(Integer id){
        return drugMapper.selectAdviceById(id);
    }


}
